In [1]:
import html5lib
In [2]:
import os, geopandas as gpd

zelanda_estados=gpd.read_file(os.path.join("maps","New Zeland_Divisions","New Zeland_Divisions.shp"))
In [3]:
#Verificando si el tipo de datos que tenemos es GeoDataframe
type(zelanda_estados)
Out[3]:
geopandas.geodataframe.GeoDataFrame
In [4]:
#Dimensiones
zelanda_estados.shape
Out[4]:
(735, 10)
In [5]:
#Nombre de las columnas
zelanda_estados.columns
Out[5]:
Index(['f_code', 'coc', 'nam', 'laa', 'pop', 'ypc', 'adm_code', 'salb', 'soc',
       'geometry'],
      dtype='object')
In [6]:
#Verificando estas columnas
zelanda_estados.head()
Out[6]:
f_code coc nam laa pop ypc adm_code salb soc geometry
0 FA001 NZL North Auckland UNK -99999999 0 UNK UNK NZL POLYGON ((174.73207 -36.95977, 174.73417 -36.9...
1 FA001 NZL North Auckland UNK -99999999 0 UNK UNK NZL POLYGON ((174.46234 -36.96070, 174.46139 -36.9...
2 FA001 NZL North Auckland UNK -99999999 0 UNK UNK NZL POLYGON ((174.46615 -36.95461, 174.46595 -36.9...
3 FA001 NZL North Auckland UNK -99999999 0 UNK UNK NZL POLYGON ((174.67965 -36.87457, 174.67873 -36.8...
4 FA001 NZL North Auckland UNK -99999999 0 UNK UNK NZL POLYGON ((174.67457 -36.86594, 174.67463 -36.8...
In [7]:
# Información 
zelanda_estados.info()
<class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 735 entries, 0 to 734
Data columns (total 10 columns):
 #   Column    Non-Null Count  Dtype   
---  ------    --------------  -----   
 0   f_code    735 non-null    object  
 1   coc       735 non-null    object  
 2   nam       735 non-null    object  
 3   laa       735 non-null    object  
 4   pop       735 non-null    int64   
 5   ypc       735 non-null    int64   
 6   adm_code  735 non-null    object  
 7   salb      735 non-null    object  
 8   soc       735 non-null    object  
 9   geometry  735 non-null    geometry
dtypes: geometry(1), int64(2), object(7)
memory usage: 57.6+ KB
In [8]:
#Borrando todas las columnas excepto "nam" y "geometry" para mayor orden
byeColumns=['f_code', 'coc','laa', 'pop', 'ypc', 'adm_code', 'salb', 'soc']

#El resultado
zelanda_estados.drop(columns=byeColumns,inplace=True)
#Ahora
zelanda_estados
Out[8]:
nam geometry
0 North Auckland POLYGON ((174.73207 -36.95977, 174.73417 -36.9...
1 North Auckland POLYGON ((174.46234 -36.96070, 174.46139 -36.9...
2 North Auckland POLYGON ((174.46615 -36.95461, 174.46595 -36.9...
3 North Auckland POLYGON ((174.67965 -36.87457, 174.67873 -36.8...
4 North Auckland POLYGON ((174.67457 -36.86594, 174.67463 -36.8...
... ... ...
730 Southland POLYGON ((167.59838 -44.74032, 167.59864 -44.7...
731 Southland POLYGON ((167.91376 -44.68164, 167.91137 -44.6...
732 Southland POLYGON ((167.91154 -44.67762, 167.91051 -44.6...
733 Southland POLYGON ((167.63308 -44.64638, 167.63244 -44.6...
734 Southland POLYGON ((169.26659 -46.62536, 169.26534 -46.6...

735 rows × 2 columns

In [9]:
# Verificando si todos los valores existen
zelanda_estados[zelanda_estados.isna().any(axis=1)]
Out[9]:
nam geometry
In [10]:
#Visualizando las divisiones administrativas de Nueva Zelanda
zelanda_estados.plot()
Out[10]:
<Axes: >
In [11]:
#Abriendo los shapefile de las vías férreas y los aeropuertos
railroad=gpd.read_file(os.path.join("maps","New Zeland_Railroads","New Zeland_Railroads.shp"))
aero=gpd.read_file(os.path.join("maps","New Zeland_Airports","New Zeland_Airports.shp"))
In [12]:
#Analizando la información dentro de "railroad"
railroad.info()
<class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 152 entries, 0 to 151
Data columns (total 6 columns):
 #   Column    Non-Null Count  Dtype   
---  ------    --------------  -----   
 0   f_code    152 non-null    object  
 1   exs       152 non-null    int64   
 2   fco       152 non-null    int64   
 3   loc       152 non-null    int64   
 4   soc       152 non-null    object  
 5   geometry  152 non-null    geometry
dtypes: geometry(1), int64(3), object(2)
memory usage: 7.3+ KB
In [13]:
#Analizando la información
aero.info()
<class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 26 entries, 0 to 25
Data columns (total 8 columns):
 #   Column    Non-Null Count  Dtype   
---  ------    --------------  -----   
 0   f_code    26 non-null     object  
 1   iko       26 non-null     object  
 2   ita       26 non-null     object  
 3   nam       26 non-null     object  
 4   use       26 non-null     int64   
 5   zv3       26 non-null     int64   
 6   soc       26 non-null     object  
 7   geometry  26 non-null     geometry
dtypes: geometry(1), int64(2), object(5)
memory usage: 1.8+ KB
In [14]:
railroad.head()
Out[14]:
f_code exs fco loc soc geometry
0 AN010 28 3 0 NZL LINESTRING (174.53098 -36.89878, 174.53213 -36...
1 AN010 28 3 0 NZL LINESTRING (175.18607 -37.54066, 175.18526 -37...
2 AN010 28 3 0 NZL LINESTRING (171.68327 -43.91570, 171.68401 -43...
3 AN010 28 3 0 NZL LINESTRING (176.89090 -39.49532, 176.89083 -39...
4 AN010 28 3 0 NZL LINESTRING (174.89642 -41.03294, 174.89695 -41...
In [15]:
aero.head()
Out[15]:
f_code iko ita nam use zv3 soc geometry
0 GB005 UNK UNK Whangarei Airport 0 -999 NZL POINT (174.36235 -35.77013)
1 GB005 UNK UNK Bay of Islands Airport 0 -999 NZL POINT (173.91303 -35.25739)
2 GB005 UNK UNK Invercargill Airport 0 -999 NZL POINT (168.31838 -46.41389)
3 GB005 UNK UNK Dunedin Airport 0 -999 NZL POINT (170.19998 -45.92799)
4 GB005 UNK UNK Alexandra Airport 0 -999 NZL POINT (169.36758 -45.20826)
In [16]:
#Verificando el nombre de las columnas
aero.columns
Out[16]:
Index(['f_code', 'iko', 'ita', 'nam', 'use', 'zv3', 'soc', 'geometry'], dtype='object')
In [17]:
#Borrando todas las columnas excepto "nam" y "geometry" para mayor rapidez en el manejo de datos
byeColumns=['f_code', 'iko', 'ita','use', 'zv3', 'soc']

#El resultado
aero.drop(columns=byeColumns,inplace=True)
#Ahora
aero
Out[17]:
nam geometry
0 Whangarei Airport POINT (174.36235 -35.77013)
1 Bay of Islands Airport POINT (173.91303 -35.25739)
2 Invercargill Airport POINT (168.31838 -46.41389)
3 Dunedin Airport POINT (170.19998 -45.92799)
4 Alexandra Airport POINT (169.36758 -45.20826)
5 Queenstown Airport POINT (168.74567 -45.01871)
6 Oamaru Airport POINT (171.08512 -44.96801)
7 Timaru Airport POINT (171.22831 -44.30225)
8 Aoraki/Mount Cook Airport POINT (170.13650 -43.76507)
9 Christchurch International Airport POINT (172.53358 -43.48853)
10 Hokitika Airport POINT (170.98699 -42.71159)
11 Westport Airport POINT (171.58307 -41.73827)
12 Nelson Airport POINT (173.22662 -41.29456)
13 Marlborough Airport POINT (173.86942 -41.51793)
14 Wellington International Airport POINT (174.80615 -41.32602)
15 Wanganui Airport POINT (175.02488 -39.96476)
16 Palmerston North Airport POINT (175.61668 -40.32037)
17 Hawke's Bay Airport POINT (176.86537 -39.46503)
18 New Plymouth Airport POINT (174.17850 -39.00630)
19 Taupo Airport POINT (176.08399 -38.74336)
20 Hamilton Airport POINT (175.33153 -37.86397)
21 Rotorua Airport POINT (176.31594 -38.10756)
22 Whakatane Airport POINT (176.91698 -37.92132)
23 Tauranga Airport POINT (176.19455 -37.67295)
24 Gisborne Airport POINT (177.97495 -38.66034)
25 Auckland International Airport POINT (174.78734 -37.00912)
In [18]:
#Visualizando aeropuertos
aero.plot()
Out[18]:
<Axes: >
In [19]:
#Verificando el nombre de las columnas
railroad.columns
Out[19]:
Index(['f_code', 'exs', 'fco', 'loc', 'soc', 'geometry'], dtype='object')
In [20]:
#Borrando todas las columnas excepto  "geometry"
byeColumns=['f_code', 'exs', 'fco', 'loc', 'soc']

#El resultado
railroad.drop(columns=byeColumns,inplace=True)
#Ahora
railroad
Out[20]:
geometry
0 LINESTRING (174.53098 -36.89878, 174.53213 -36...
1 LINESTRING (175.18607 -37.54066, 175.18526 -37...
2 LINESTRING (171.68327 -43.91570, 171.68401 -43...
3 LINESTRING (176.89090 -39.49532, 176.89083 -39...
4 LINESTRING (174.89642 -41.03294, 174.89695 -41...
... ...
147 LINESTRING (171.56359 -42.83246, 171.56332 -42...
148 LINESTRING (168.33036 -46.59095, 168.32716 -46...
149 LINESTRING (168.79676 -46.28225, 168.79800 -46...
150 LINESTRING (176.16884 -37.66855, 176.16866 -37...
151 LINESTRING (172.69631 -43.56909, 172.69829 -43...

152 rows × 1 columns

In [21]:
#Visualizando las vías férreas
railroad.plot()
Out[21]:
<Axes: >
In [22]:
#Cambiando el estilo visual para los polígonos - división administrativa:
zelanda_estados.plot(facecolor="violet", edgecolor='black', linewidth=0.1) 
Out[22]:
<Axes: >
In [23]:
#Cambiando el estilo para las líneas - vías férreas: 
railroad.plot(edgecolor='slategrey', linewidth=1, linestyle='dashed')
Out[23]:
<Axes: >
In [24]:
#Cambiando el estilo visual para los puntos - aeropuertos:
aero.plot(marker='3', color='navy', markersize=3,alpha=0.8) 
Out[24]:
<Axes: >
In [25]:
zelanda_estados.crs
Out[25]:
<Geographic 2D CRS: GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84" ...>
Name: WGS 84
Axis Info [ellipsoidal]:
- lon[east]: Longitude (degree)
- lat[north]: Latitude (degree)
Area of Use:
- undefined
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
In [26]:
aero.crs
Out[26]:
<Geographic 2D CRS: GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84" ...>
Name: WGS 84
Axis Info [ellipsoidal]:
- lon[east]: Longitude (degree)
- lat[north]: Latitude (degree)
Area of Use:
- undefined
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
In [27]:
railroad.crs
Out[27]:
<Geographic 2D CRS: GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84" ...>
Name: WGS 84
Axis Info [ellipsoidal]:
- lon[east]: Longitude (degree)
- lat[north]: Latitude (degree)
Area of Use:
- undefined
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
In [28]:
#Como vemos, estos coinciden, aún así cambiaremos el crs para que coincidan como método de prevención
railroad=railroad.to_crs(zelanda_estados.crs)
aero=aero.to_crs(zelanda_estados.crs)
In [29]:
#Mapa completo del país partido en divisiones administrativas, vías férreas y aeropuertos
base = zelanda_estados.plot(facecolor="white", edgecolor='black', linewidth=0.1,figsize=(12,12))
aero.plot(marker='3', color='navy', markersize=4,alpha=0.8,
            ax=base) # encima de...
railroad.plot(edgecolor='red', linewidth=0.4,
            ax=base)# encima de...
Out[29]:
<Axes: >
In [30]:
import folium

#Crear un objeto de mapa
m= folium.Map(zoom_start=10)

#Agregando cosas al mapa
folium.GeoJson(zelanda_estados.to_json(),name='Divisiones_administ',style_function=lambda feature: {'color':'red'}).add_to(m)
folium.GeoJson(railroad.to_json(),name='Railroads',style_function=lambda x: {'fillColor':'white','color':'green'}).add_to(m)
folium.GeoJson(aero.to_json(),name='Aeropuertos').add_to(m)

#El mapa
m
Out[30]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [34]:
#Recortamos solo para Gisborne
Gisborne=zelanda_estados[zelanda_estados.nam=='Gisborne']
In [35]:
#Clip (cortar), mask=división administrativa, cortar todos los puntos que están encima de otros cosos que no sea la división administrativa seleccionada o mask
aero_clipped = gpd.clip(gdf=aero,
                          mask=Gisborne)
railroad_clipped = gpd.clip(gdf=railroad,
                               mask=Gisborne)
In [36]:
base = Gisborne.plot(facecolor="greenyellow", edgecolor='black', linewidth=0.4,figsize=(5,5))
aero_clipped.plot(marker='3', color='blue', markersize=70,ax=base)
railroad_clipped.plot(edgecolor='red', linewidth=0.9,ax=base)
Out[36]:
<Axes: >
In [37]:
#Coordenada de Gisborne obtenida de https://www.geodatos.net/en/coordinates/new-zealand/gisborne
gisborneCoord=[-38.65333, 178.00417] 
In [38]:
#Crear un objeto de mapa
m= folium.Map(zoom_start=8,location=gisborneCoord)

#Agregando cosas al mapa
folium.GeoJson(Gisborne.to_json(),name='Divisiones_administ',style_function=lambda feature: {'color':'red'}).add_to(m)
folium.GeoJson(railroad_clipped.to_json(),name='Railroads',style_function=lambda x: {'fillColor':'white','color':'green'}).add_to(m)
folium.GeoJson(aero_clipped.to_json(),name='Aeropuertos').add_to(m)

#El mapa
m
Out[38]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [41]:
#Guardar el mapa como una imagen
import matplotlib.pyplot as plt

base = Gisborne.plot(facecolor="greenyellow", edgecolor='black', linewidth=0.4,figsize=(5,5))
map2=aero_clipped.plot(marker='3', color='blue', markersize=70,ax=base)
mapEnd=railroad_clipped.plot(edgecolor='red', linewidth=0.9,ax=base)
plt.savefig(os.path.join("maps",'mapEnd.jpg'))
In [42]:
zelanda_estados.to_file(os.path.join("maps","Nueva_Zelanda.gpkg"), layer='Divisiones_administ', driver="GPKG")
aero.to_file(os.path.join("maps","Nueva_Zelanda.gpkg"), layer='Aeropuertos', driver="GPKG")
railroad.to_file(os.path.join("maps","Nueva_Zelanda.gpkg"), layer='Railroads', driver="GPKG")
In [43]:
Nueva_Zelanda= 'https://github.com/lizBadillo/simpleplot/raw/main/maps/Nueva_Zelanda.gpkg'
In [44]:
from  fiona import listlayers

listlayers(Nueva_Zelanda)
Out[44]:
['Divisiones_administ', 'Aeropuertos', 'Railroads']
In [47]:
zelanda_estados=gpd.read_file(Nueva_Zelanda,layer='Divisiones_administ')
aero=gpd.read_file(Nueva_Zelanda,layer='Aeropuertos')
railroad=gpd.read_file(Nueva_Zelanda,layer='Railroads')
In [48]:
base = zelanda_estados.plot(facecolor='gainsboro')
aero.plot(ax=base, markersize=0.5, color='red') 
railroad.plot(ax=base, linewidth=0.5)
Out[48]:
<Axes: >
In [ ]: